/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConsultaEmpleados.java
 *
 * Created on 11-oct-2011, 10:32:26
 */
package apiformularios.form;

import api.dom.Camion;
import api.dom.Utilitaria;
import java.awt.print.PrinterException;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable.PrintMode;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis
 */
public class FrmConsultaEmpleados extends javax.swing.JInternalFrame {

    /** Creates new form FrmConsultaEmpleados */
    public FrmConsultaEmpleados() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        jComboBox1.setModel(Utilitaria.listarCamionesCombo((DefaultComboBoxModel) jComboBox1.getModel()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnatras = new javax.swing.JButton();
        btnporpantalla = new javax.swing.JButton();
        btnimprimir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jComboBox1 = new javax.swing.JComboBox();
        total = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        total1 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setMinimumSize(new java.awt.Dimension(24, 32));
        setNextFocusableComponent(jDateChooser1);
        setNormalBounds(new java.awt.Rectangle(0, 0, 650, 570));
        setOpaque(true);
        setPreferredSize(new java.awt.Dimension(652, 592));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setPreferredSize(new java.awt.Dimension(650, 71));

        jLabel6.setBackground(new java.awt.Color(51, 255, 51));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel6.setText("Lista Gastos");
        jLabel6.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addContainerGap(387, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 70));

        jLabel1.setText("Dede fecha:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jLabel2.setText("Camion :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 80, -1, -1));

        btnatras.setText("Atras");
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        getContentPane().add(btnatras, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 500, 85, 40));

        btnporpantalla.setText("Por pantalla");
        btnporpantalla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnporpantallaActionPerformed(evt);
            }
        });
        getContentPane().add(btnporpantalla, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 500, -1, 40));

        btnimprimir.setText("Imprimir");
        btnimprimir.setEnabled(false);
        btnimprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnimprimirActionPerformed(evt);
            }
        });
        getContentPane().add(btnimprimir, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 500, 87, 40));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "N°", "Fecha", "Rubro", "Importe", "Camion"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, 630, 380));

        jDateChooser1.setNextFocusableComponent(jDateChooser2);
        getContentPane().add(jDateChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, 140, -1));

        jDateChooser2.setNextFocusableComponent(jComboBox1);
        getContentPane().add(jDateChooser2, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 80, 130, -1));

        jComboBox1.setName(""); // NOI18N
        jComboBox1.setNextFocusableComponent(btnporpantalla);
        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(526, 80, 110, -1));

        total.setFont(new java.awt.Font("Tahoma", 1, 24));
        total.setText("0");
        getContentPane().add(total, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 500, -1, 40));

        jLabel4.setText("Hasta fecha:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 80, -1, -1));

        total1.setFont(new java.awt.Font("Tahoma", 1, 24));
        total1.setText("Total: $ ");
        getContentPane().add(total1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 500, -1, 40));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnporpantallaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnporpantallaActionPerformed
        // TODO add your handling code here:
        try {
            Camion objC = (Camion) jComboBox1.getSelectedItem();
            
            Utilitaria.limpiarJtables((DefaultTableModel) jTable1.getModel());
            Date desde = jDateChooser1.getDate();
            Date hasta = jDateChooser2.getDate();
            Utilitaria.listaGastosEntre2Fechas(((DefaultTableModel) this.jTable1.getModel()), desde, hasta, objC);
            this.total.setText(String.valueOf(Utilitaria.listaGastosEntre2FechasTotal(desde, hasta, objC)));
            btnimprimir.setEnabled(true);
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(rootPane, "Seleccione las fechas entre las cuales se va a consultar");
        }
    }//GEN-LAST:event_btnporpantallaActionPerformed

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
        this.dispose();

        // TODO add your handling code here:
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnimprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnimprimirActionPerformed
        Date date = new Date();
        Date date2 = new Date();
        date = (jDateChooser1.getDate());
        date2 = (jDateChooser2.getDate());
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String fechadesde = dateFormat.format(date);
        String fechahasta = dateFormat.format(date2);
        MessageFormat encabezado1 = new MessageFormat("Gastos del Camion  " + jComboBox1.getSelectedItem().toString());
        MessageFormat encabezado2 = new MessageFormat("Desde: " + fechadesde + "   Hasta: " + fechahasta);
                if (jTable1.getRowCount() == 0) {
            JOptionPane.showMessageDialog(rootPane, "No se puede imprimir. \n Esta en blanco la lista");
        } else {
            try {
                // TODO add your handling code here:
                jTable1.print(PrintMode.FIT_WIDTH, encabezado1,encabezado2);
            } catch (PrinterException ex) {
                Logger.getLogger(FrmConsultaEmpleados.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnimprimirActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnatras;
    private javax.swing.JButton btnimprimir;
    private javax.swing.JButton btnporpantalla;
    private javax.swing.JComboBox jComboBox1;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel total;
    private javax.swing.JLabel total1;
    // End of variables declaration//GEN-END:variables
}
